package org.dk.jpa;

import org.dk.jpa.annotation.RepositoryMetadata;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;

import javax.persistence.EntityManager;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author jiangjielong on 2016-12-21.
 * @version 1.0
 * <p>
 * Repository自定义工厂类
 * 通过在spring boot 启动类总使用一下注解调用
 * @EnableJpaRepositories(repositoryFactoryBeanClass = BaseRepositoryFactoryBean.class)
 * xml配置
 * <jpa:repositories factory-class="com.ygsoft.microApp.core.spring.jpa.BaseRepositoryFactoryBean"/>
 * </p>
 */
public class BaseRepositoryFactoryBean<T extends JpaRepository<S, ID>, S, ID extends Serializable>
        extends JpaRepositoryFactoryBean<T, S, ID> {

    /**
     * Creates a new {@link JpaRepositoryFactoryBean} for the given repository interface.
     *
     * @param repositoryInterface must not be {@literal null}.
     */
    public BaseRepositoryFactoryBean(Class<? extends T> repositoryInterface) {
        super(repositoryInterface);
    }

    @Override
    protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
        return new BaseRepositoryFactory(entityManager);
    }

    private static class BaseRepositoryFactory<T, ID extends Serializable> extends JpaRepositoryFactory {


        public BaseRepositoryFactory(EntityManager em) {
            super(em);
        }

        @Override
        protected <T, ID extends Serializable> SimpleJpaRepository<?, ?> getTargetRepository(RepositoryInformation information, EntityManager entityManager) {
            Annotation[] annotations = information.getRepositoryInterface().getAnnotations();
            for (Annotation annotation : annotations) {
                if (annotation instanceof RepositoryMetadata) {
                    RepositoryMetadata repositoryMetadata = (RepositoryMetadata) annotation;
                    try {
                        Constructor c = repositoryMetadata.repositoryBeanClass().getConstructor(information.getDomainType().getClass(), EntityManager.class);
                        return (SimpleJpaRepository<?, ?>) c.newInstance(information.getDomainType(), entityManager);
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InstantiationException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
            }
            return new BaseRepositoryImpl<T, ID>((Class<T>) information.getDomainType(), entityManager);
        }

        @Override
        protected Class<?> getRepositoryBaseClass(org.springframework.data.repository.core.RepositoryMetadata metadata) {
            Annotation[] annotations = metadata.getRepositoryInterface().getAnnotations();
            for (Annotation annotation : annotations) {
                if (annotation instanceof RepositoryMetadata) {
                    RepositoryMetadata repositoryMetadata = (RepositoryMetadata) annotation;
                    return repositoryMetadata.repositoryBeanClass();
                }
            }
            return BaseRepositoryImpl.class;
        }
    }
}